package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.JT808MsgBean;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;
import com.uzhie.jt808.util.JT808ProtocolUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 终端应答的目录结构
 * Created by lilongfei on 2018/7/29.
 */
public class JT_1205 implements JT808MsgBody {

    private static Logger logger = Logger.getLogger(JT_1205.class);

    //消息流水号
    int serialNumber;
    //资源总数
    int count;
    //远程设备音视频目录
    List<JT_1205_Catalog> catalog;

    public JT_1205(){}

    public JT_1205(byte[] body){
        ReadFromBytes(body);
    }

    /**
     * 获取流水号
     * @param body
     * @return
     */
    public static int getNumber(byte[] body){
        MyBuffer buffer = new MyBuffer(body);
        byte[] numbers = buffer.gets(2);
        int number = BitConverter.ToUInt16(numbers,0);
        return number;
    }

    public int getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(int serialNumber) {
        this.serialNumber = serialNumber;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<JT_1205_Catalog> getCatalog() {
        return catalog;
    }

    public void setCatalog(List<JT_1205_Catalog> catalog) {
        this.catalog = catalog;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();
        byte[] numbers = BitConverter.GetBytes(getSerialNumber());
        buffer.put(numbers[2]);
        buffer.put(numbers[3]);
        buffer.put(getCount());
        try {
            for (int i = 0; i < getCount(); i++) {
                buffer.put(getCatalog().get(i).WriteToBytes());
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("1025命令序列化错误 : " + getCatalog().toString());
        }
        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        logger.info("收到远程查询目录结果，消息体长度为: " +body.length);
        MyBuffer buffer = new MyBuffer(body);
        byte[] numbers = buffer.gets(2);
        int number = BitConverter.ToUInt16(numbers,0);
        setSerialNumber(number);
        int count = BitConverter.ToUInt32(buffer.gets(4),0);
        setCount(count);
        List<JT_1205_Catalog> catalogs = new ArrayList<>();
        try {
            for (int i = 0 ;i < getCount(); i ++ ){
                byte[] bytes = buffer.gets(28);
                JT_1205_Catalog catalog = new JT_1205_Catalog();
                catalog.ReadFromBytes(bytes);
                catalogs.add(catalog);
            }
            setCatalog(catalogs);
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
            logger.error("命令1025获取的请求返回长度存在问题,包长度不完整, 当前条数应有" + getCount()+ "条"
            + ",但数组长度只有 " + body.length
            );
        }
        logger.info("远程目录查询结果: " +toString());
    }

    public static void  main(String[] arge){
        JT_1205 order = new JT_1205();
        order.setSerialNumber(1);
        List<JT_1205_Catalog> catalogs = new ArrayList<>();
        JT_1205_Catalog catalog = new JT_1205_Catalog();
        catalog.setChannel(1);
        catalog.setWarns(0);
        catalog.setLocal(1);
        catalog.setStream(1);
        catalog.setType(0);
        catalog.setSize(1024);
        catalog.setBeginTime("2018-07-29 00:00:00");
        catalog.setEndTime("2018-07-29 00:01:00");
        catalogs.add(catalog);
        order.setCount(catalogs.size());
        order.setCatalog(catalogs);
        byte[] bodys = order.WriteToBytes();
        System.out.println(bodys.length + "");
        JT808MsgBean bean = JT808ProtocolUtil.build808Obj(JT808Constant.MSG_ID.DEV_REQ_PLAYBACK_CATALOG,1,"15639932744",order.WriteToBytes());


    }

    public String toString(){
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(String.format(
                "请求流水号：%1$s,目录总数：%2$s",
                getSerialNumber(), getCount()));
        for (int i= 0 ;i<getCount();i++){
            if (catalog !=null && catalog.size() >= i){
                sBuilder.append("\n");
                JT_1205_Catalog clg = catalog.get(i);
                sBuilder.append("[");
                sBuilder.append(clg.toString());
                sBuilder.append("]");
            }
        }
        return sBuilder.toString();
    }

}
